package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.BorrowTenderModel;

/**
 * 借贷管理-投标信息管理
 * 
 * @author xx
 * @version 2.0
 * @since 2014年4月17日
 */
public class ManageTenderAction extends BaseAction<BorrowTenderModel> implements ModelDriven<BorrowTenderModel> {

	@Resource
	private BorrowTenderService borrowTenderService;

	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * 投标信息展示
	 * 
	 * @return
	 */
	@Action("/modules/loan/tender/tenderManager")
	public String tenderManager() throws Exception {

		return "tenderManager";
	}

	/**
	 * 投标信息列表
	 * 
	 * @return
	 */
	@Action("/modules/loan/tender/tenderList")
	public void tenderList() throws Exception {
		model.setSize(paramInt("rows"));
		String status = request.getParameter("status");
		if (status == null) {
			model.setStatus(-1); // 标记为全部
		}
		String category = request.getParameter("category");
		if (category == null) {
			model.setCategory(98); // 默认不显示体验金投标
		}
		PageDataList<BorrowTenderModel> list = borrowTenderService.list(model);
		data.put("total", list.getPage().getTotal());
		data.put("rows", list.getList());
		printJson(getStringOfJpaObj(data));
	}
	/**
	 * 导出报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/loan/tender/exportExcelTender")
	public void exportExcelTender() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		// 最多出5000条记录
		model.setSize(5000);
		List<BorrowTenderModel> list = borrowTenderService.list(model).getList();
		String title = "投资记录Excel表";
		String[] hearders = new String[] { "编号", "用户名", "真实姓名", "标名", "期数", "投资金额", "利息", "待收总额", "投资时间", "投资状态"};// 表头数组
		String[] fields = new String[] { "id", "userName", "realName", "borrowName", "period", "money",
				"interest", "repaymentAccount", "addTime", "statusString" };// 对象属性数组
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
}
